import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Solution827 {

    static class Bing{
        int[] b;
        int[] size;

        public Bing(int n){
            b=new int[n];
            for (int i=0;i<n;i++){
                b[i]=i;
            }
            size=new int[n];
            Arrays.fill(size,1);
        }

        public void union(int x,int y){
            int xx=find(x);
            int yy=find(y);
            if (xx!=yy){
                b[xx]=yy;
                size[yy]+=size[xx];
            }
        }

        public int find(int x){
            return b[x]==x?x:(b[x]=find(b[x]));
        }
    }

    public int largestIsland(int[][] grid) {
        int n=grid.length;
        int[] dx=new int[]{0,1,0,-1};
        int[] dy=new int[]{1,0,-1,0};
        Bing bing=new Bing(n*n);
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
                if (grid[i][j]==1){
                    if (i!=n-1 && grid[i+1][j]==1){
                        bing.union(i*n+j,(i+1)*n+j);
                    }
                    if (j!=n-1 && grid[i][j+1]==1){
                        bing.union(i*n+j,i*n+j+1);
                    }
                }
            }
        }
        int max=Arrays.stream(bing.size).max().getAsInt();
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
                if (grid[i][j]==0){
                    int ans=1;
                    Set<Integer> set=new HashSet<>();
                    for (int k=0;k<4;k++){
                        int xx=i+dx[k];
                        int yy=j+dy[k];
                        if (xx>=0 && xx<n && yy>=0 && yy<n && grid[xx][yy]==1 && !set.contains(bing.find(xx*n+yy))){
                            ans+=bing.size[bing.find(xx*n+yy)];
                            set.add(bing.find(xx*n+yy));
                        }
                    }
                    max=Math.max(max,ans);
                }
            }
        }
        return max;
    }

    public static void main(String[] args) {
        System.out.println(new Solution827().largestIsland(new int[][]{{1,1},{1,0}}));
    }
}
